package org.droid.campus.api.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.droid.campus.api.action.GetAdAction;
import org.droid.campus.api.action.GetCanteenAction;
import org.droid.campus.api.action.GetCatDishAction;
import org.droid.campus.api.action.GetShopCatAction;
import org.droid.campus.api.action.GetShopsAction;
import org.droid.campus.common.ActionHandler;
import org.droid.campus.common.Enums.ActionStatus;
import org.droid.campus.common.RequestTypeException;
import org.droid.campus.common.X;
import org.droid.campus.util.RespStatusBuilder;

/**
 * Servlet Filter implementation class ParameterValidatorFilter
 */

public class ParameterValidatorFilter implements Filter {
	private final static Logger log = Logger.getLogger(ParameterValidatorFilter.class) ;

	

    /**
     * Default constructor. 
     */
    public ParameterValidatorFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		// pass the request along the filter chain
		ActionHandler action = null ;
		try {
			action = routeRequestAction(request) ;
			boolean prepareResult = action.prepare((HttpServletRequest)request) ;
			if(!prepareResult){
				//prepared error ...
				throw new RequestTypeException("Invalid Prepared Status Returns : 001") ;
			} else {
				chain.doFilter(request, response) ;	
			}
		} catch (RequestTypeException e) {
			// TODO Auto-generated catch block
			if(log.isTraceEnabled()){
				log.trace(e) ;
			}
			response.getWriter().print(
					RespStatusBuilder.message(
							ActionStatus.REQUEST_TYPE_ERROR.inValue(),
							ActionStatus.REQUEST_TYPE_ERROR.getDescription()
									+ " : " + e.getMessage()));
			return ;
		} finally {
			/*if(actionFucker != null){
				actionFucker.releaseResource() ;
			}*/
			action = null ;
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		//this.registerActions(fConfig.getServletContext()) ;
	}
	
	
	public static ActionHandler routeRequestAction(ServletRequest request) throws RequestTypeException {
		HttpServletRequest req = (HttpServletRequest) request;
		ServletContext servletContext = req.getSession().getServletContext() ;
		String actionName = request.getParameter("forword") ;
		
		if(actionName.equalsIgnoreCase(X.ActionType.GetAd)){
			return new GetAdAction(servletContext,actionName) ;
		}
		if(actionName.equalsIgnoreCase(X.ActionType.GetCanteen)){
			return new GetCanteenAction(servletContext,actionName) ;
		}
		if(actionName.equalsIgnoreCase(X.ActionType.GetShops)){
			return new GetShopsAction(servletContext,actionName);
		}
		if(actionName.equalsIgnoreCase(X.ActionType.GetShopCat)){
			return new GetShopCatAction(servletContext,actionName);
		}
		if(actionName.equalsIgnoreCase(X.ActionType.GetCatDish)){
			return new GetCatDishAction(servletContext,actionName);
		}
		
		throw new RequestTypeException("CampusOrder API has no such Request Type !") ;
	}
}
